package com.sinothk.gpserver.modules.biz.controller;

import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.biz.entity.ScoreAdminLogEntity;
import com.sinothk.gpserver.modules.biz.service.IScoreAdminLogService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  1. 积分-管理员积分交易记录
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-08-29
 */
@Api(tags = "积分-管理员积分交易记录")
@RequestMapping("/scoreAdminLog")
@ResponseResult()
@RestController
public class ScoreAdminLogController extends BaseController {

    @Resource
    private IScoreAdminLogService scoreAdminLogService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<ScoreAdminLogEntity> page(@RequestBody PageVo<ScoreAdminLogEntity> pageVo) {
        return scoreAdminLogService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public ScoreAdminLogEntity get(@ApiParam("id") @PathVariable String id) {
        return scoreAdminLogService.getById(id);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public String add(@RequestBody ScoreAdminLogEntity scoreAdminLogEntity) {
        LoginEntity loginUser = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        scoreAdminLogEntity.setCreateBy(loginUser.getUserName());
        scoreAdminLogService.save(scoreAdminLogEntity);
        return "新增成功";
    }


    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public ScoreAdminLogEntity update(@RequestBody ScoreAdminLogEntity scoreAdminLogEntity) {
        if (StringUtil.isEmpty(scoreAdminLogEntity.getId())) {
            throw new BizException("id不能为空");
        }
        LoginEntity loginUser = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        scoreAdminLogEntity.setUpdateBy(loginUser.getUserName());
        scoreAdminLogService.updateById(scoreAdminLogEntity);
        return scoreAdminLogService.getById(scoreAdminLogEntity.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody ScoreAdminLogEntity scoreAdminLogEntity) {
        LoginEntity loginUser = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (StringUtil.isEmpty(scoreAdminLogEntity.getId())) {
            scoreAdminLogEntity.setCreateBy(loginUser.getUserName());
            scoreAdminLogService.save(scoreAdminLogEntity);
            return "新增成功";
        } else {
            scoreAdminLogEntity.setUpdateBy(loginUser.getUserName());
            scoreAdminLogService.updateById(scoreAdminLogEntity);
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        scoreAdminLogService.removeById(id);
        return "已逻辑删除";
    }
}
